package com.jiuyin.dianjing.ui.fragment.main;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.jiuyin.dianjing.R;
import com.jiuyin.dianjing.app.HelperApplication;
import com.jiuyin.dianjing.event.RefreshEvent;
import com.jiuyin.dianjing.model.SettingBasicInfo;
import com.jiuyin.dianjing.ui.activity.community.CollegeListActivity;
import com.jiuyin.dianjing.ui.activity.setting.MyPersonalActivity;
import com.jiuyin.dianjing.ui.activity.setting.SettingActivity;
import com.jiuyin.dianjing.ui.activity.setting.SettingAttentionActivity;
import com.jiuyin.dianjing.ui.activity.setting.SettingCollectionActivity;
import com.jiuyin.dianjing.ui.activity.setting.SettingMyFansActivity;
import com.jiuyin.dianjing.ui.activity.setting.SettingMyHomePageActivity;
import com.jiuyin.dianjing.ui.activity.setting.SettingSubscriptionActivity;
import com.jiuyin.dianjing.ui.activity.team.MyTeamsActivity;
import com.jiuyin.dianjing.ui.fragment.base.BaseLazyFragment;
import com.jiuyin.dianjing.util.DisplayUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONObject;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;

import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_POST;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_TOKEN;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_TYPE;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_USERID;

/**
 * 主界面(我的)
 */
public class FragmentFive extends BaseLazyFragment {
    private static String ZERO = "0";
    @BindView(R.id.tv_title)
    TextView mTitle;
    @BindView(R.id.img_head)
    ImageView mImgHead;
    @BindView(R.id.tv_nick_name)
    TextView tvNickName;
    @BindView(R.id.tv_id_value)
    TextView tvId;
    @BindView(R.id.tv_attention_value)
    TextView tvAttention;
    @BindView(R.id.tv_fans_value)
    TextView tvFans;
    @BindView(R.id.tv_collect_value)
    TextView tvCollect;
    @BindView(R.id.tv_subscription_value)
    TextView tvSubscription;
    @BindView(R.id.srl_record_layout)
    SmartRefreshLayout mSrlRecordLayout;
    SettingBasicInfo mBasicInfo;

    private static final int REQUEST_CODE_MY_PERSONAL = 102;
    private static final int REQUEST_CODE_MY_ATTENTION = 103;

    @Override
    public void fetchData() {
        HashMap<String, Object> params = new HashMap<>();
        params.put(KEY_TOKEN, HelperApplication.getToken());
        JSONObject data = new JSONObject(params);
        getApiHelper().getMyInformationDetail(data.toString(), basicInfo -> {
            mSrlRecordLayout.finishRefresh();
            if(null != basicInfo) {
                mBasicInfo = basicInfo;
                Glide.with(getContext())
                        .load(basicInfo.header)
                        .placeholder(R.drawable.img_header_placeholder)
                        .override(DisplayUtil.dp2px(getContext(), 44), DisplayUtil.dp2px(getContext(), 44))
                        .into(mImgHead);
                tvNickName.setText(basicInfo.nickname);
                tvAttention.setText(getString(R.string.attention, basicInfo.attention_num));
                tvFans.setText(getString(R.string.fans, basicInfo.fans_num));
                tvCollect.setText(getString(R.string.collect, basicInfo.collect_num));
                tvSubscription.setText(getString(R.string.subscribe, basicInfo.subscribe_num));
                tvId.setText("ID:" + basicInfo.id);
                HelperApplication.setNickName(basicInfo.nickname);
                HelperApplication.setMobile(basicInfo.mobile);
                HelperApplication.setID(basicInfo.id);
            }
        });
    }

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_five;
    }

    @Override
    protected void initData() {
        mSrlRecordLayout.setEnableLoadMore(false);
        mSrlRecordLayout.setOnRefreshListener(refreshLayout -> fetchData());
    }

    @Override
    protected void initView() {
        mTitle.setText(R.string.my_title);
        tvId.setText("ID:");
        tvNickName.setText(HelperApplication.getNickName());
    }

    @OnClick({R.id.img_head, R.id.tv_id_value, R.id.tv_attention_value, R.id.tv_collect_value, R.id.tv_fans_value, R.id.tv_subscription_value,
            R.id.ll_my_school, R.id.ll_my_my_team, R.id.img_setting_enter,})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_head: {
                Intent intent = new Intent(getContext(), MyPersonalActivity.class);
                intent.putExtra(KEY_POST, mBasicInfo);
                startActivityForResult(intent, REQUEST_CODE_MY_PERSONAL);
            }
            break;
            case R.id.tv_id_value: {
                Intent intent = new Intent(getContext(), SettingMyHomePageActivity.class);
                intent.putExtra(KEY_USERID, "");
                startActivity(intent);
            }
            break;
            case R.id.tv_fans_value://粉丝数
                goTargetActivity(SettingMyFansActivity.class);
                break;
            case R.id.tv_attention_value://关注数
                Intent intent = new Intent(getContext(), SettingAttentionActivity.class);
                startActivityForResult(intent, REQUEST_CODE_MY_ATTENTION);
                break;
            case R.id.tv_subscription_value://订阅
                goTargetActivity(SettingSubscriptionActivity.class);
                break;
            case R.id.tv_collect_value://收藏数
                goTargetActivity(SettingCollectionActivity.class);
                break;
            case R.id.ll_my_school://我的高校
                goTargetActivity(CollegeListActivity.class);
                break;
            case R.id.ll_my_my_team://我的战队
                Bundle bundle = new Bundle();
                bundle.putString(KEY_TYPE, "1");
                bundle.putString(KEY_USERID, "");
                goTargetActivity(MyTeamsActivity.class, bundle);
                break;
            case R.id.img_setting_enter://设置
                goTargetActivity(SettingActivity.class);
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (requestCode == REQUEST_CODE_MY_PERSONAL || requestCode == REQUEST_CODE_MY_ATTENTION) {
            if (resultCode == Activity.RESULT_OK) {
                fetchData();
            }
        }
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        EventBus.getDefault().register(this);
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(RefreshEvent event) {
        if (event.clazzName.equals(getClass().getSimpleName())) {
            fetchData();
        }
    }
}
